package lucky.xiong.certifacte;

import com.alibaba.fastjson.JSONObject;

import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayInputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;


public class X509CertificateParser {
    public static void main(String[] args) {
        String base64Certificate = "-----BEGIN CERTIFICATE-----\n" +
                "MIIDoDCCAoigAwIBAgIJYpKUIQEHKBQIMA0GCSqGSIb3DQEBCwUAMD4xCzAJBgNV\n" +
                "BAYTAkNOMQ0wCwYDVQQKDARUZXN0MQ0wCwYDVQQLDAR0ZXN0MREwDwYDVQQDDAhS\n" +
                "U0EgVEVTVDAeFw0yMjExMzAwMjQzMjlaFw0yMzExMzAwMjQzMjlaMFoxCzAJBgNV\n" +
                "BAYTAkNOMQ8wDQYDVQQKDAbkuK3lrokxCjAIBgNVBAsMATIxGTAXBgkqhkiG9w0B\n" +
                "CQEWCjkxQDE2My5jb20xEzARBgNVBAMMClJTQV8yXzIwNDgwggEiMA0GCSqGSIb3\n" +
                "DQEBAQUAA4IBDwAwggEKAoIBAQDHE0ViphJmyJQ3KwreFf8vL8K+3kwo4XV6e56x\n" +
                "cgl0ggLByyA02wk4CjPQlFrii5RjBsTZEselm4eBt+aT/TTiYmaWgQnuK8lPKGy1\n" +
                "EimurgmdNcTXOa4BqQVvA3N6vgb7O6uZaXbV1Onop1fpLvWQVzPZFGED8xFSg7OM\n" +
                "BkqVQWNRMWuThRfFwIsce170B6RJYI0zNDhVr9LQYSSyvFhvu8xb0BPj8KbGKB2/\n" +
                "7wMWnWiuoEF3G0uB6Nyxt8isCrZyuYATcl6Xp4Ood19GicNGdjEsNvKjJTyGWESO\n" +
                "+w4a6xwufRfnjlCDjN+dIJhOwCCJQRhoUfft9s6QmK8bWu+3AgMBAAGjgYQwgYEw\n" +
                "HQYDVR0OBBYEFMp2DCqeqp50AYH9gVpIwrmBVX5uMA4GA1UdDwEB/wQEAwIGwDAf\n" +
                "BgNVHSMEGDAWgBRzQ1mppuLRCZIXjHEmqepOBMsgvzAdBgNVHSUEFjAUBggrBgEF\n" +
                "BQcDAQYIKwYBBQUHAwIwEAYDVR0RAQH/BAYwBIECMTEwDQYJKoZIhvcNAQELBQAD\n" +
                "ggEBACzMkEhqAk3MXEWI2GLlLSwexYvBUECJjvyK0/S4wfyx/p5oIn/4T+OJRru2\n" +
                "926tXNKl0DRzIzPMui5iM5DPVJRjjtwVO5gguSFoJUQo/PKfzqWD6KJyrqXzoxw9\n" +
                "OOwroRqC0mK3zOERaL91uQk87rb2F4TNmbmJxi9D3jpsUHda9ERiv4M3IVjKun9c\n" +
                "rkVHGU8zr+szOAuhNZYU7NCdI/dhLG8/N6cpNBlVl9VVxpYBiDqBq6cOAxHVt9dO\n" +
                "ayQxMpDWPkQf4MLGBNEJIF8IB7a4vAqH/0Qyka/Spsrgpg69zkq/rQv/zvt5LVse\n" +
                "cZISXfQjVYcQg5pqEn+HSGpJp8A=\n" +
                "-----END CERTIFICATE-----";

        // 创建一个JSON对象并添加字段
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("signData", base64Certificate);

        // 将JSON对象转换为字符串
        String jsonString = jsonObject.toString();

        // 打印JSON字符串
        System.out.println(jsonString);
        try {
            // 创建证书工厂
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

            // 从证书字符串中创建证书对象
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64Certificate.getBytes());
            X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);

            // 获取证书信息
            System.out.println("Subject: " + certificate.getSubjectDN());
            String subjectStr = certificate.getSubjectX500Principal().toString();
            System.out.println(subjectStr);

            String[] pairs = subjectStr.split(",");

            for (String pair : pairs) {
                String trim = pair.trim();
                String[] keyValue = trim.split("=", 2);
                if (keyValue.length == 2) {
                    String key = keyValue[0].trim();
                    String value = keyValue[1].trim();
                    System.out.println("Key: " + key + ", Value: " + value);
                }
            }
            System.out.println("Issuer: " + certificate.getIssuerDN());
            System.out.println("Serial Number: " + certificate.getSerialNumber());

            // 将证书转换为字节数组形式
            byte[] certBytes = certificate.getEncoded();

            // 输出证书字节数组长度
            System.out.println("Certificate Byte Array Length: " + certBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
